#include <nice2d/input.h>
#include <nice2d/application.h>

class InputMessageListener : public MessageListener
{
public:
    InputMessageListener() = delete;
    InputMessageListener(Input *input):
        inputPointer(input)
    {
    }
public:
    bool onListener(std::shared_ptr<Message> message)override
    {
        assert(inputPointer);

        if (message->getMessageType() != MessageType_SFML)
            return false;

        auto sfmlMessage = std::dynamic_pointer_cast<SFMLMessage>(message);
        auto event = sfmlMessage->getEvent();
        if (event.type == sf::Event::Closed)
            inputPointer->onWindowClose();
        else if (event.type == sf::Event::Resized)
            inputPointer->onWindowSizeChanged(event.size.width, event.size.height);
        else if (event.type == sf::Event::TextEntered)
            inputPointer->onTextInput(event.text.unicode);
        else if (event.type == sf::Event::KeyPressed)
            inputPointer->onKeyPressed(event.key);
        else if (event.type == sf::Event::KeyReleased)
            inputPointer->onKeyReleased(event.key);
        else if(event.type == sf::Event::MouseButtonPressed)
            inputPointer->onMousePressed(event.mouseButton);
        else if(event.type == sf::Event::MouseButtonReleased)
            inputPointer->onMouseReleased(event.mouseButton);
        else
            return false;

        return true;

    }
private:
    Input *inputPointer = nullptr;
};

Input::Input()
{
}

Input::~Input()
{
}

void Input::setInputMessageTarget(ObjectPointer node)
{
    if (node) {
        auto listener = std::make_shared<InputMessageListener>(this);
        node->registerMessageListener(listener);
    }
}

void Input::onWindowClose()
{
}

void Input::onWindowSizeChanged(int32_t width, int32_t height)
{
}

void Input::onTextInput(uint32_t ch)
{
}

void Input::onKeyPressed(const sf::Event::KeyEvent &event)
{
}

void Input::onKeyReleased(const sf::Event::KeyEvent &event)
{
}

void Input::onMousePressed(const sf::Event::MouseButtonEvent &button)
{

}

void Input::onMouseReleased(const sf::Event::MouseButtonEvent &button)
{

}
